package com.boot.security.server.dao.mffl;

import java.util.List;
import java.util.Map;

import com.boot.security.server.model.mffl.Vo.MfflConsultationReplyVo;
import org.apache.ibatis.annotations.*;

import com.boot.security.server.model.mffl.MfflConsultationReply;

@Mapper
public interface MfflConsultationReplyDao {

    @Select("select * from mffl_consultation_reply t where t.consultation_id = #{id} and t.append = 0 AND t.delete_state =1")
    List<MfflConsultationReply> getConsultationIdList(Long id);


    @Select("select * from mffl_consultation_reply t where t.consultation_id = #{param1} and t.append = #{param2} AND t.delete_state =1")
    List<MfflConsultationReply> getConsultationIdAppendList(Integer param1,Long param2);


    @Select("select * from mffl_consultation_reply t where t.id = #{id} AND t.delete_state =1")
    MfflConsultationReply getById(Long id);

    @Delete("delete from mffl_consultation_reply where id = #{id} AND delete_state =1")
    int delete(Long id);

    int update(MfflConsultationReply mfflConsultationReply);
    
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into mffl_consultation_reply(userid, lawyer, user_or_lawyer, consultation_id, service_type, reply, image_url, pay_id, pay_number, pay_merchant_number, append, createTime, updateTime, delete_state, deleteTime,Rewardnumber,replynumber) values(#{userid}, #{lawyer}, #{userOrLawyer}, #{consultationId}, #{serviceType}, #{reply}, #{imageUrl}, #{payId}, #{payNumber}, #{payMerchantNumber}, #{append}, #{createTime}, #{updateTime}, #{deleteState}, #{deleteTime}, #{rewardnumber}, #{replynumber})")
    int save(MfflConsultationReply mfflConsultationReply);
    
    int count(@Param("params") Map<String, Object> params);

    List<MfflConsultationReplyVo> list(@Param("params") Map<String, Object> params, @Param("offset") Integer offset, @Param("limit") Integer limit);

    /*查询当前用户所要设为最佳答案的评论是否存在*/
    @Select("SELECT * FROM mffl_consultation_reply mcr,mffl_consultation mc WHERE mc.id = mcr.consultation_id AND mc.userid = #{params.userid} AND mc.id = #{params.consultation} AND mcr.id = #{params.consultationreply}  AND mcr.append = 0 AND mcr.delete_state = 1")
    MfflConsultationReply getBestAnswer(@Param("params") Map<String,Integer> params);

    /**
     * 获取最早一次回复
     * @param id
     * @return
     */
    @Select("select id,userid from mffl_consultation_reply where consultation_id = #{id} and append = 0 AND delete_state = 1 ORDER BY createTime ASC LIMIT 1;")
    MfflConsultationReply getEarliest(@Param("id") Long id);

}
